package com.tjrac.generalpermissions.dao;

import com.tjrac.generalpermissions.domain.Permission;
import com.tjrac.generalpermissions.domain.Role;
import com.tjrac.generalpermissions.domain.RolePermission;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author ZeNing
 * @create 2020-11-27 21:45
 */
@Repository
public interface RolePermissionDao {

    @Select("select * from rolepermission")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(
                    column = "rid",
                    property = "role",
                    javaType = Role.class,
                    one = @One(select = "com.tjrac.generalpermissions.dao.RoleDao.queryById")
            ),
            @Result(
                    column = "pid",
                    property = "permission",
                    javaType = Permission.class,
                    one = @One(select = "com.tjrac.generalpermissions.dao.PermissionDao.queryById")
            )
    })
    List<RolePermission> queryAll();

    @Select("select * from rolepermission where `id` = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(
                    column = "rid",
                    property = "role",
                    javaType = Role.class,
                    one = @One(select = "com.tjrac.generalpermissions.dao.RoleDao.queryById")
            ),
            @Result(
                    column = "pid",
                    property = "permission",
                    javaType = Permission.class,
                    one = @One(select = "com.tjrac.generalpermissions.dao.PermissionDao.queryById")
            )
    })
    RolePermission queryById(int id);

    @Select("select * from rolepermission where `rid` = #{rid}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(
                    column = "rid",
                    property = "role",
                    javaType = Role.class,
                    one = @One(select = "com.tjrac.generalpermissions.dao.RoleDao.queryById")
            ),
            @Result(
                    column = "pid",
                    property = "permission",
                    javaType = Permission.class,
                    one = @One(select = "com.tjrac.generalpermissions.dao.PermissionDao.queryById")
            )
    })
    List<RolePermission> queryByRid(int rid);

    @Select("select * from rolepermission where `pid` = #{pid}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(
                    column = "rid",
                    property = "role",
                    javaType = Role.class,
                    one = @One(select = "com.tjrac.generalpermissions.dao.RoleDao.queryById")
            ),
            @Result(
                    column = "pid",
                    property = "permission",
                    javaType = Permission.class,
                    one = @One(select = "com.tjrac.generalpermissions.dao.PermissionDao.queryById")
            )
    })
    List<RolePermission> queryByPid(int pid);

    @Insert("insert into rolepermission(`rid`, `pid`) values(#{role.id}, #{permission.id})")
    int addRolePermission(RolePermission rolePermission);

    @Insert("insert into rolepermission(`rid`, `pid`) values(#{arg0}, #{arg1})")
    int addRolePermissionByRP(int rid, int pid);

    @Delete("delete from rolepermission where `id` = #{id}")
    int delRolePermission(int id);

    @Delete("delete from rolepermission where `rid` = #{arg0} and `pid` = #{arg1}")
    int delRolePermissionByRP(int rid, int pid);

    @Update("update rolepermission set `rid` = #{role.id}, `pid` = #{permission.id} where `id` = #{id}")
    int modifyById(RolePermission rolePermission);

}
